#include "DialogFilter.h"
#include "MainFrame.h"
DialogFilter::DialogFilter( wxWindow* parent, int order )
:
DialogFilterTemplate( parent )
{
	isPosFilter=false;
	
	if(order)		
		isPosFilter =true;
		
	
	getValues();
	
	if(order)
		panelGrayScale->Hide();
	
	this->ShowModal();
}

void DialogFilter::OnButtonCancelClick( wxCommandEvent& event )
{
	this->EndModal(1);
}

void DialogFilter::OnButtonOkClick( wxCommandEvent& event )
{
	MainFrame* parent = (MainFrame*)this->GetParent();
	double value1,value2,value3; 
	wxString value;
	
	if(!isPosFilter)
	{
		//save erode iterations
		value = textCtrlErodeIts->GetValue();
		value1 = atof(value.char_str());
		parent->setPreErodeIterations((int)value1);
		
		//save smooth filter type
		if(comboBoxSmthType->GetSelection()!=-1)
			parent->setPreSmoothType(comboBoxSmthType->GetSelection());
		
		//save params  of smooth filter
		value = textCtrlParam1->GetValue();
		value1 = atof(value.char_str());
		
		value = textCtrlParam2->GetValue();
		value2 = atof(value.char_str());
		
		value = textCtrlParam3->GetValue();
		value3 = atof(value.char_str());
		
		parent->setPreSmoothParams(value1,value2,value3);
		
		//save lowgray
		value = textCtrlLowGray->GetValue();
		value1 = atof(value.char_str());
		
		//save highgray
		value = textCtrlHighGray->GetValue();
		value2 = atof(value.char_str());
		
		parent->setBckgndThresholds((int)value1, (int)value2);
		
		
	}
	else
	{
		//save erode iterations
		value = textCtrlErodeIts->GetValue();
		value1 = atof(value.char_str());
		parent->setPosErodeIterations((int)value1);
		
		//save smooth filter type
		if(comboBoxSmthType->GetSelection()!=-1)
			parent->setPosSmoothType(comboBoxSmthType->GetSelection());
		
		//save params  of smooth filter
		value = textCtrlParam1->GetValue();
		value1 = atof(value.char_str());
		
		value = textCtrlParam2->GetValue();
		value2 = atof(value.char_str());
		
		value = textCtrlParam3->GetValue();
		value3 = atof(value.char_str());
		
		parent->setPosSmoothParams(value1,value2,value3);

	}
	
	this->EndModal(1);
}

void DialogFilter::getValues()
{
	MainFrame* parent = (MainFrame*)this->GetParent();
	char* value = new char[30];
	wxString wxValue;
	
	
	if(!isPosFilter)
	{
		//obtain erode iterations	
		sprintf(value, "%d", parent->getPreErodeIterations());
		
		wxValue = wxValue.FromAscii(value);
		textCtrlErodeIts->SetValue(wxValue);
		
		//obtain smooth type
		comboBoxSmthType->SetSelection(parent->getPreSmoothType());
		
		//obtain param1 of smooth filter
		sprintf(value, "%f", *(parent->getPreSmoothParams()));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam1->SetValue(wxValue);
		
		//obtain param2 of smooth filter
		sprintf(value, "%f", *(parent->getPreSmoothParams()+1));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam2->SetValue(wxValue);
		
		//obtain param3 of smooth filter
		sprintf(value, "%f", *(parent->getPreSmoothParams()+2));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam3->SetValue(wxValue);
	
		//obtain lowgray of background filter
		sprintf(value, "%d", *(parent->getBckgndThresholds()));
		wxValue = wxValue.FromAscii(value);
		textCtrlLowGray->SetValue(wxValue);
		
		//obtain highgray of background filter
		sprintf(value, "%d", *(parent->getBckgndThresholds()+1));
		wxValue = wxValue.FromAscii(value);
		textCtrlHighGray->SetValue(wxValue);
	}
	else
	{
		//obtain erode iterations	
		sprintf(value, "%d", parent->getPosErodeIterations());
		
		wxValue = wxValue.FromAscii(value);
		textCtrlErodeIts->SetValue(wxValue);
		
		//obtain smooth type
		comboBoxSmthType->SetSelection(parent->getPosSmoothType());
		
		//obtain param1 of smooth filter
		sprintf(value, "%f", *(parent->getPosSmoothParams()));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam1->SetValue(wxValue);
		
		//obtain param2 of smooth filter
		sprintf(value, "%f", *(parent->getPosSmoothParams()+1));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam2->SetValue(wxValue);
		
		//obtain param3 of smooth filter
		sprintf(value, "%f", *(parent->getPosSmoothParams()+2));
		wxValue = wxValue.FromAscii(value);
		textCtrlParam3->SetValue(wxValue);
	

	}
	
	delete value;
	
		
}


